package com.study.controller;

import com.study.model.Echarts;
import com.study.model.User;
import com.study.service.LogService;
import com.study.service.UserService;
import com.study.util.getUserInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @author king james
 * @create 2021-09-07-{TIME}
 */
@Controller
/**
 * 登录界面
 */
public class HomeController {

    @Autowired
    private LogService logService;

    @Autowired
    private UserService userService;

    @RequestMapping(value="/login",method= RequestMethod.GET)
    public String login(){
        return "login";
    }

    /**
     * 登录时用户的密码用户名合法性校验
      * @param request 设置提示信息
     * @param user 用户信息
     * @return
     */
    @RequestMapping(value="/login",method=RequestMethod.POST)
    public String login(HttpServletRequest request, User user, Model model){
        if (StringUtils.isEmpty(user.getUsername()) || StringUtils.isEmpty(user.getPassword())) {
            request.setAttribute("msg", "用户名或密码不能为空！");
            return "login";
        }
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token=new UsernamePasswordToken(user.getUsername(),user.getPassword());
        try {
            subject.login(token);
            return "redirect:usersPage";
        }catch (LockedAccountException lae) {
            token.clear();
            request.setAttribute("msg", "用户已经被锁定不能登录，请与管理员联系！");
            return "login";
        } catch (AuthenticationException e) {
            token.clear();
            request.setAttribute("msg", "用户或密码不正确！");
            return "login";
        }
    }

    @RequestMapping(value = "/getUserName")
    @ResponseBody
    public String getUserName(){
        getUserInfo getUserInfo = new getUserInfo();//获取到用户名
        //通过用户名获取到用户的角色
        String userRole = userService.getUserRole(getUserInfo.getUserName());
        System.out.println(userRole + "角色");
        String userNameRole = "当前用户：" + getUserInfo.getUserName() +  "|" + "角色：" + userRole;
        return userNameRole;
    }

    /**
     * 用户管理界面
     * @return
     */
    @RequestMapping(value={"/usersPage",""})
    public String usersPage(){
        return "user/users";
    }

    /**
     * 角色管理界面
     * @return
     */
    @RequestMapping("/rolesPage")
    public String rolesPage(){
        return "role/roles";
    }

    /**
     * 资源管理界面
     * @return
     */
    @RequestMapping("/resourcesPage")
    public String resourcesPage(){
        return "resources/resources";
    }

    /**
     * 日志管理界面
     */
    @RequestMapping("logPage")
    public String logPage(){
        return "log/log";
    }

    /**
     * 图形展示
     * @return
     */
    @RequestMapping("/echartsPage")
    public String echartsPage(){
        return "echarts/echarts";
    }
    /**
     *错误页
     * @return
     */
    @RequestMapping("/403")
    public String forbidden(){
        return "403";
    }
}
